<!--
 * @Description: 文本输入弹窗
 * @Author: liyujie
 * @Date: 2021/08/31
 * @Last Modified by: liyujie
 * @Last Modified time: 2021/08/31
-->
<template>
    <section class="wrapper">
        <el-dialog
            :title="title"
            :visible.sync="dialogVisible"
            width="640px"
            :close-on-click-modal="false"
            @close="beforeClose"
        >
            <div class="dialog-content">
                <div
                    class="show-text"
                    v-if="showText"
                >
                    <div class="text-wrapper">{{ showText }}</div>
                </div>
                <div v-else>
                    <div
                        class="message-view"
                        v-if="message"
                    >
                        {{ message }}
                    </div>
                    <el-input
                        v-model="text"
                        type="textarea"
                        :autosize="{ minRows: 4, maxRows: 6}"
                        :placeholder="placeholder"
                        :maxlength="maxlength"
                        show-word-limit
                    />
                </div>
            </div>
            <div class="dialog-footer">
                <template v-if="showText">
                    <el-button
                        type="primary"
                        size="middle"
                        :style="{width: '120px'}"
                        @click="close"
                    >
                        我知道了
                    </el-button>
                </template>
                <template v-else>
                    <el-button
                        size="medium"
                        type="default"
                        :style="{width: '120px', marginRight: '32px'}"
                        v-if="hasCancelButton"
                        @click="close"
                    >
                        {{ cancelButtonText }}
                    </el-button>
                    <el-button
                        type="primary"
                        size="medium"
                        :style="{width: '120px'}"
                        :disabled="!text"
                        @click="handleConfirm"
                    >
                        {{ confirmButtonText }}
                    </el-button>
                </template>

            </div>
        </el-dialog>
    </section>
</template>

<script>
/**
 * ZdPrompt 文本输入与显示组件
 *
 * @description 自定义element MessageBox组件
 * @author liyujie
 * @date 2021/09/02
 * @param title - 标题
 * @param message - 提示信息
 * @param maxlength - 最大输入数量
 * @param placeholder
 * @param showText 显示的文本
 * @param confirmButtonText - 确认按钮文本
 * @param cancelButtonText - 取消按钮文本
 * @param hasCancelButton - 是否有取消按钮
 */
import Vue from "vue";

const Dialog = {
    name: 'ZdPrompt',
    props: {
        title: {
            type: String,
            default: '提示'
        },
        message: {
            type: String,
            default: ''
        },
        confirmButtonText: {
            type: String,
            default: '确定'
        },
        cancelButtonText: {
            type: String,
            default: '取消'
        },
        hasCancelButton: {
            type: Boolean,
            default: true
        },
        maxlength: {
            // eslint-disable-next-line no-bitwise
            type: Number | String,
            default: 20
        },
        placeholder: {
            type: String,
            default: '请输入...'
        },
        showText: {
            type: String,
            default: ''
        },
        defaultValue: {
            type: String,
            default: ''
        }
    },
    data() {
        return {
            dialogVisible: false,
            text: ''
        };
    },
    mounted() {
        this.text = this.defaultValue;
    },
    methods: {

        /**
         * 关闭弹窗
         */
        close() {
            this.dialogVisible = false;
        },

        /**
         * 弹窗关闭前
         */
        beforeClose() {
            this.text = '';
            this.$emit('close');
        },

        /**
         * 确定
         */
        handleConfirm() {
            this.$emit('confirm', this.text);
        }
    },
};

export default Dialog;

const promptDialog = function (config) {
    return new Promise((resolve) => {
        const DialogConstructor = Vue.extend(Dialog);
        const instance = new DialogConstructor({
            propsData: config,
        }).$mount();

        instance.$on("close", () => {
            setTimeout(() => {
                document.body.removeChild(instance.$el);
                instance.$destroy();
            }, 300);
            resolve(false);
        });
        instance.$on("confirm", (data) => {
            instance.close();
            resolve(data);
        });
        document.body.appendChild(instance.$el);
        instance.dialogVisible = true;
    });
};

function registryPrompt() {
    Vue.prototype.$zdPrompt = promptDialog;
}

export { registryPrompt };
</script>

<style lang="less" scoped>
.wrapper {
    .dialog-content {
        .show-text {
            display: flex;
            flex-direction: column;
            align-items: center;
            .text-wrapper {
                width: 100%;
                padding: 24px;
                background: #FBFBFB;
                border-radius: 4px;
                border: 1px solid #EEEEEE;
                line-height: 20px;
                margin: 4px 34px;
            }
        }
        .message-view {
            margin-bottom: 16px;
            font-size: 14px;
            font-family: PingFangSC-Regular, PingFang SC;
            font-weight: 400;
            color: #333333;
            &::before {
                content: "*";
                color: #FF3030;
            }
        }
    }
    .dialog-footer {
        display: flex;
        align-items: center;
        justify-content: center;
        margin-top: 24px;
    }
    /deep/ .el-dialog__header {
        padding-top: 10px;
        box-shadow: 0 1px 0 0 rgba(233, 233, 233, 1);
        display: flex;
        align-items: center;
        background: #FAFAFA;
        border-radius: 4px 4px 0 0;

        .el-dialog__title {
            font-weight: 500;
            color: #333;
            font-size: 14px;
        }

        .el-icon-close:before {
            color: rgb(194, 194, 194);
            font-size: 17px;
            font-weight: bold;
        }

        .el-dialog__headerbtn {
            top: 14px;
        }
    }
    /deep/ .el-dialog__body {
        padding: 32px 64px;
    }

    /deep/ .el-textarea__inner {
        resize: none;
    }
}
</style>
